<?php
/**
* NextgenAlbumWidget extends WP_Widget
*
* This implements a WordPress widget designed to show galleries from a selected album
*/
class NextgenAlbumWidget extends WP_Widget
{
    public $name = 'Nextgen Album Widget';
    public $description = 'Shows Nextgen galleries from a selected album';
    /* List all controllable options here along with a
    default value.
    The values can be distinct for each instance of the
    widget. */
    public $control_options = array(
        'title' => 'Title',
        'albumid' => ''
    );
    //!!! Magic Functions
    // The constructor.
    function __construct()
    {
        $widget_options = array(
        'classname' => __CLASS__,
        'description' => $this->widget_desc,
        );
        parent::__construct( __CLASS__, $this->name,$widget_options,$this->control_options);
    }
    
    /**
    * Displays the widget form in the manager, used for editing its
    settings
    *
    * @param array $instance The settings for the particular
    instance of the widget
    * @return none No value is returned directly, but form elements
    are printed.
    */
    function form( $instance )
    {
        global $wpdb; 
        $albumlist = $wpdb->get_results("SELECT * FROM $wpdb->nggalbum ORDER BY id");
        
        echo '<label for="'.$this->get_field_id('title').'">Titel</label><br/>';
        echo '<input type="text" name="'.$this->get_field_name('title').'" id="NextgenAlbumWidget-title" value="'.$instance['title'].'"><br/>';
        
        echo '<label for="'.$this->get_field_id('albumid').'">Vælg Album</label><br/>';
        echo '<select name="'.$this->get_field_name('albumid').'">';
        foreach( $albumlist as $album ) {
            echo '<option value="'.$album->id.'"'.  selected($instance['albumid'], $album->id) .'>' . $album->name . '</option>';
        }
        echo '</select>';
    } 

    /**
     * Update $instance values
     * 
     * @param array $new_instance New instances
     * @param array $old_instance Saved instances
     * @return none No direct output
     */
    function update($new_instance, $old_instance) {
        
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['albumid'] = $new_instance['albumid'];
        
        return $instance;
    }
    
    /**
    * Displays content to the front-end.
    *
    * @param array $args Display arguments
    * @param array $instance The settings for the particular instance
    of the widget
    * @return none No direct output. This should instead print output
    directly.
    */
    function widget($args, $instance)
    {
        global $nggdb;
        extract($args);
	$title = apply_filters('widget_title', $instance['title']);
        
        $galleries = array();

        $album = $nggdb->find_album($instance['albumid']);

        foreach( $album->gallery_ids as $galleryid ){
            $gallery = $nggdb->find_gallery($galleryid);
            $image = $nggdb->find_image( $gallery->previewpic );
            $galleries[$galleryid]['title'] = $gallery->title;
            $galleries[$galleryid]['url'] = get_bloginfo('url') . '/film-og-billeder/galleri/?gallery=' . $galleryid;
            $galleries[$galleryid]['previewpic'] = $image->thumbURL;
            $x++;
            if ($x == 6)
                break;
        }
        
	$output = "\n" . $args['before_widget'] . "\n" . $args['before_title'] . $title . $args['after_title'] . "\n" . "    <div class=\"gallery-pbb-side-menu-container\">\n       <ul id=\"gallery-pbb-side-menu\" class=\"menu\">\n";
        foreach($galleries as $category){
            $output .= '            <a href="' . $category['url'] . '" title="' . $category['title'] . '"><img src="' . $category['previewpic'] . '" width="92" height="92" alt="' . $category['title'] . '"></a>';
            $output .= "\n";
        }
        $output .= "\n      </ul>\n     </div>\n" . $args['after_widget'] . "\n";
        echo $output;
    }
    
    
    
    //!!! Static Functions
    static function register_this_widget()
    {
    register_widget(__CLASS__);
    }
}
/* EOF */